package com.qianfeng.sks.findingfood.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;

import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.sks.findingfood.R;
import com.qianfeng.sks.findingfood.adapter.HeadAdapter;
import com.qianfeng.sks.findingfood.adapter.HomeAdapter;
import com.qianfeng.sks.findingfood.bean.AppBean;
import com.qianfeng.sks.findingfood.bean.ResultListListgoodsItem;
import com.qianfeng.sks.findingfood.bean.ResultListchannelItem;
import com.qianfeng.sks.findingfood.url.Path;
import com.qianfeng.sks.findingfood.utils.HttpIconTools;
import com.qianfeng.sks.findingfood.utils.HttpTools;
import com.qianfeng.sks.findingfood.utils.MyGridView;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2 {
    @ViewInject(R.id.home_page_PullToRefreshListView)
    private PullToRefreshListView lv;
    private HomeAdapter adapter;
    private ArrayList<ResultListListgoodsItem> listgoodsItem = new ArrayList<>();
    private HttpHandler<String> httpHandler;
    private int currentPage = 9;
    private boolean isClear = true;


    private BitmapUtils bitmapUtils;

    /**
     * 添加头部的headView
     */
    @ViewInject(R.id.item_head_gridView)
    private MyGridView gridView;
    private ArrayList<ResultListchannelItem> listchannelItem = new ArrayList<>();
    private HeadAdapter headAdapter;
    private HttpHandler<String> httpheadHandler;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, null);
        bitmapUtils = new HttpIconTools(getActivity()).getIntance();
        initView(view, inflater);
        loadDatas(currentPage, isClear);
        return view;
    }

    public View addHeadGridView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.item_fragment_home_headview, null);
        ViewUtils.inject(this, view);
        initGridView();
        loadGridView();
        return view;
    }

    private void loadGridView() {
        TypeReference<AppBean<ResultListchannelItem>> type = new TypeReference<AppBean<ResultListchannelItem>>() {
        };
        httpheadHandler = HttpTools.getHttpTools().
                getDataFromServer(Path.HOME_PAGE_FIRST, headAdapter, listchannelItem, false, type);

    }

    private void initGridView() {
        headAdapter = new HeadAdapter(getActivity(), listchannelItem, bitmapUtils);
        gridView.setAdapter(headAdapter);
    }


    private void loadDatas(int currentPage, boolean isClear) {
        TypeReference<AppBean<ResultListListgoodsItem>> type = new TypeReference<AppBean<ResultListListgoodsItem>>() {
        };
        httpHandler = HttpTools.getHttpTools().
                getDataFromServer(Path.getHomePageSecond(currentPage), adapter, listgoodsItem, isClear, type);
        lv.onRefreshComplete();
    }

    private void initView(View v, LayoutInflater inflater) {
        ViewUtils.inject(this, v);
        lv.setMode(PullToRefreshBase.Mode.BOTH);
        lv.setOnRefreshListener(this);
        adapter = new HomeAdapter(getActivity(), listgoodsItem, bitmapUtils);
        ListView listView = lv.getRefreshableView();
        listView.addHeaderView(addHeadGridView(inflater));
        lv.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, true, false));
        lv.setAdapter(adapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        httpHandler.cancel();
        httpheadHandler.cancel();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        currentPage++;
        isClear = false;
        loadDatas(currentPage, false);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        currentPage = 9;
        loadDatas(currentPage, isClear);
    }
}
